<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="商户简称" prop="shortName">
        <name-input v-model="queryParams.shortName" placeholder="请输入商户简称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商户类型" prop="merchantType">
        <el-select v-model="queryParams.merchantType" placeholder="请选择商户类型" clearable>
          <el-option label="企业" value="COMPANY" />
          <el-option label="个体户" value="INDIVIDUAL" />
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="customerName">
        <name-input v-model="queryParams.customerName" placeholder="请输入公司名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="法人姓名" prop="legalName">
        <el-input v-model="queryParams.legalName" placeholder="请输入法人姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="businessPhone">
        <el-input v-model="queryParams.businessPhone" placeholder="请输入联系人手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系人邮箱" prop="businessEmail">
        <el-input v-model="queryParams.businessEmail" placeholder="请输入联系人邮箱" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="入网状态" prop="step">
        <el-select v-model="queryParams.step" placeholder="请选择入网状态" clearable>
          <el-option v-for="item in baofuMerchantStep" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="银行账户状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择银行账户状态" clearable>
          <el-option v-for="item in baofuAccountStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="统一会员号" prop="uid">
        <el-input v-model="queryParams.uid" placeholder="请输入统一会员号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属平台" v-if="user.userId === 1" prop="platformNickName">
        <el-input v-model="queryParams.platformNickName" placeholder="请输入所属平台" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属代理" v-if="user.deptId < 101" prop="agentNickName">
        <el-input v-model="queryParams.agentNickName" placeholder="请输入所属代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属二级代理" v-if="user.deptId < 102" prop="childAgentNickName">
        <el-input v-model="queryParams.childAgentNickName" placeholder="请输入所属二级代理" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ecs:yeePay:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="店铺名称" fixed="left" width="280" align="center" prop="shortName" />
      <el-table-column label="统一会员号" prop="uid" width="150" align="center" />
      <el-table-column label="公司名称" align="center" prop="customerName" width="300" />
      <el-table-column label="所属平台" v-if="user.userId === 1" align="center" prop="platformNickName" width="210" />
      <el-table-column label="所属代理" v-if="user.deptId < 101" align="center" prop="agentNickName" width="210" />
      <el-table-column label="所属二级代理" v-if="user.deptId < 102" align="center" prop="childAgentNickName" width="210" />
      <el-table-column label="所属商户" align="center" prop="shopNickName" width="210" />
      <el-table-column label="商户类型 " align="center" prop="merchantType">
        <template slot-scope="{ row }">
          <span v-if="row.merchantType === 'INDIVIDUAL'">个体户</span>
          <span v-if="row.merchantType === 'COMPANY'">企业</span>
        </template>
      </el-table-column>
      <el-table-column label="法人姓名" align="center" prop="legalName" width="110" />
      <el-table-column label="联系人手机号" align="center" prop="businessPhone" width="110" />
      <el-table-column label="联系人邮箱" align="center" prop="businessEmail" width="180" />
      <el-table-column label="入网状态" align="center" prop="step" width="180">
        <template slot-scope="{ row }">
          <template v-for="s in baofuMerchantStep">
            <template v-if="s.value === row.step">{{ s.label }}</template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="失败原因" align="center" prop="stepRemark" width="180" />
      <el-table-column label="签约状态" align="center" prop="merchantApplyStep" width="100">
        <template slot-scope="{row}">
          <template v-for="s in baofuMerchantApplyStep">
            <template v-if="s.value === row.merchantApplyStep">{{ s.label }}</template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="产品开通状态" align="center" prop="productApplyStep" width="100">
        <template slot-scope="{row}">
          <template v-for="s in baofuProductApplyStep">
            <template v-if="s.value === row.productApplyStep">{{ s.label }}</template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="审核信息" align="center" prop="auditFailMsg" width="180" />
      <el-table-column label="银行账户开通状态" align="center" prop="status" width="120">
        <template slot-scope="{row}">
          <template v-for="s in baofuAccountStatus">
            <template v-if="s.value === row.status">{{ s.label }}</template>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="银行账户" align="center" prop="relAccountNo" width="120">
        <template slot-scope="{row}">
          {{ (row.status === 'S') ? row.relAccountNo : '' }}
        </template>
      </el-table-column>
      <el-table-column label="银行账户开通信息" align="center" prop="returnMsg" width="180" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180" />
      <el-table-column label="操作" fixed="right" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" icon="el-icon-edit"
            v-if="row.step === '0' || row.step === '3' || row.merchantApplyStep === '3'" @click="handleUpdate(row)"
            v-hasPermi="['ecs:yeePay:edit']">修改</el-button>
          <el-button size="mini" type="text"
            v-if="row.step === '1' || row.step === '2' || row.step === '4' || row.step === '5'"
            @click="handleDetail(row)">详情</el-button>
          <el-button size="mini" type="text" @click="handleSignMail(row)"
            v-if="user.userId === 1 && row.step === '4' && row.merchantApplyStep === '7'">重发签约邮件</el-button>
          <el-button size="mini" type="text" @click="handleBank(row)" v-hasPermi="['ecs:yeePay:edit']"
            v-if="row.step === '4' && row.merchantApplyStep === '8' && row.productApplyStep === '2'">开立银行账户</el-button>
          <el-button size="mini" type="text" @click="feeRateOpen(row)"
            v-if="user.userId === 1 && row.step === '4'">修改费率</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <BaofuMerchantEditor ref="merchantEditor" />
    <BaofuBankEditor ref="bankEditor" />

    <el-dialog :title="feeRateTitle" :visible.sync="feeRateVisible" width="35%" append-to-body>
      <el-form ref="feeRateRef" :model="feeRateForm" label-width="110px" class="form">
        <el-form-item label="费率" prop="feeRate">
          <el-input-number :step="0.1" :min="0" :max="100" :precision="2" v-model.number="feeRateForm.feeRate"
            placeholder="请输入费率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="feeRateSubmit">确 定</el-button>
        <el-button @click="feeRateCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'
import BaofuMerchantEditor from './modules/MerchantEditor.vue'
import BaofuBankEditor from './modules/BankEditor.vue'
import { baofuMerchantStep, baofuMerchantApplyStep, baofuProductApplyStep, baofuAccountStatus } from '@/api/ecs/baofu'
import { listBaofuMerchant, editBaofuMerchantFeeRate, signMailBaofuMerchant } from '@/api/ecs/baofu'
import { Message } from 'element-ui'

export default {
  name: "Baofu",
  components: { BaofuMerchantEditor, BaofuBankEditor, },
  data() {
    return {
      baofuMerchantStep,
      baofuAccountStatus,
      baofuMerchantApplyStep,
      baofuProductApplyStep,
      loading: false,
      total: 0,
      list: [],
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        shortName: '',
        merchantType: '',
        customerName: '',
        legalName: '',
        businessPhone: '',
        businessEmail: '',
        step: '',
        status: '',
        uid: '',
        platformNickName: '',
        agentNickName: '',
        childAgentNickName: '',
      },
      feeRateTitle: '',
      feeRateForm: { id: 0, feeRate: 0.0 },
      feeRateVisible: false,
      user: {}
    }
  },
  created() {
    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101) {
        getAgentUserId(this.user.userId).then(response => {
          if (response.data.agentId !== 0) {
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  methods: {
    getList() {
      listBaofuMerchant(this.queryParams).then(res => {
        if (res.code === 200) {
          this.total = res.total
          this.list = res.rows
        }
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    feeRateOpen(row) {
      this.feeRateTitle = `修改【${row.shortName}】费率`
      this.feeRateForm.id = row.id
      this.feeRateForm.feeRate = row.feeRate
      this.feeRateVisible = true
    },
    feeRateSubmit() {
      editBaofuMerchantFeeRate(this.feeRateForm).then(res => {
        if (res.code === 200) {
          this.getList()
          this.feeRateVisible = false
        }
      })
    },
    feeRateCancel() {
      this.feeRateVisible = false
      this.$refs.feeRateRef.resetFields()
    },
    handleAdd() {
      this.$refs.merchantEditor.show().then(() => {
        this.getList()
      })
    },
    handleUpdate(row) {
      this.$refs.merchantEditor.show(row.id).then(() => {
        this.getList()
      })
    },
    handleDetail(row) {
      this.$refs.merchantEditor.show(row.id, true)
    },
    handleSignMail(row) {
      signMailBaofuMerchant(row).then((res) => {
        if (res.code === 200) {
          Message({ message: res.msg, type: 'success' })
          this.getList()
        }
      })
    },
    handleBank(row) {
      this.$refs.bankEditor.show(row.id).then(() => {
        this.getList()
      })
    }
  }
}
</script>
